﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.CentralModels;
using Guline.Web.Central.Filters;
using Guline.Web.CentralModels.Object;
using System.Threading;
namespace Guline.Web.Central.Controllers
{
    [InitializeSimpleMembership]
    public class BaseController : Controller
    {
        protected List<CardType> _ListCardType;
        protected GContext db;
        // protected Account _currentAccount;
        public BaseController()
        {
            _ListCardType = new List<CardType>();
            _ListCardType.Add(new CardType() { ID = "gate", Name = "Gate" });
            _ListCardType.Add(new CardType() { ID = "mobifone", Name = "Mobifone" });
            _ListCardType.Add(new CardType() { ID = "vinaphone", Name = "Vinaphone" });
            _ListCardType.Add(new CardType() { ID = "vcoin", Name = "Vcoin" });
            _ListCardType.Add(new CardType() { ID = "zing", Name = "Zing" });

            db = new GContext();

            if (db.SmtpConfigs.Count() == 0)
            {
                db.SmtpConfigs.Add(new SmtpConfig() { ID = 1, SmtpHost = "smtp.gmail.com", SmtpPort = 587, SmtpPassword = "hienpham", SmtpUser = "info@guline.com" });
                db.SaveChanges();
            }

        }
        //
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            string cultureName = "vi-VN";
            HttpCookie cultureCookie;
            // Attempt to read the culture cookie from Request
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");

                cultureName = Languages.CultureHelper.GetDefaultCulture();
                cultureCookie["lang"] = cultureName;
                Response.Cookies.Add(cultureCookie);
            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];

                if (cultureCookie != null)
                {
                    cultureName = cultureCookie["lang"];
                    cultureName = Languages.CultureHelper.GetValidCulture(cultureName); // This is safe
                }
                else
                {
                    cultureName = Languages.CultureHelper.GetDefaultCulture();
                    cultureCookie["lang"] = cultureName;
                    Response.Cookies.Add(cultureCookie);
                }
            }

            // Modify current thread's cultures   
            System.Globalization.CultureInfo cultureme = new System.Globalization.CultureInfo(cultureName);
            //cultureme.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            // cultureme.DateTimeFormat.DateSeparator = "/";
            Thread.CurrentThread.CurrentCulture = cultureme;
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            return base.BeginExecuteCore(callback, state);
        }

    }
}
